package java8.date_time;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/**
 * @program: util
 * @description:
 * @author: zhouliangliang
 * @create: 2019-09-23 10:52
 **/
public class LocalDateTimeTest {

    /**
     * LocalDate
     * LocalTime
     * LocalDateTime
     */

    public void localDateTest() {
        LocalDate localDate = LocalDate.now();
        LocalDate d1 = localDate.withMonth(3);
        LocalDate d2 = localDate.withYear(2020);
        LocalDate d3 = localDate.withDayOfMonth(1);
        LocalDate d4 = localDate.withDayOfYear(1);
        LocalDate d5 = LocalDate.of(2019 , 9 ,30);
        LocalDate d6 = LocalDate.of(2019, Month.SEPTEMBER, 29);

        LocalDate d7 = LocalDate.parse("29/09/2019 14:50:30", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
        LocalTime t1 = LocalTime.parse("29/09/2019 14:50:30", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
        //将时间字符串转化成LocalDateTime
        LocalDateTime dt1 = LocalDateTime.parse("29/09/2019 14:50:30", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
        String dt2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));


        System.out.println(localDate);
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
        System.out.println(d4);
        System.out.println(d5);
        System.out.println(d6);
        System.out.println(d7);
        System.out.println(t1);
        System.out.println(dt1);
        System.out.println(dt2);

        //Instant 瞬间
        //chrono 记时
        Instant i1 = Instant.now();

        LocalTime lt1 = LocalTime.parse("12:30:40");
        LocalTime lt2 = LocalTime.parse("12:30:45");

        //Duration秒和纳秒衡量时间间隔
        Duration between = Duration.between(lt1, lt2);
        System.out.println(i1);
        System.out.println(between.getSeconds());
        System.out.println(between.getNano());
        Duration threeMinutes = Duration.ofMinutes(3);
        Duration threeMinutesTwo = Duration.of(3, ChronoUnit.MINUTES);
        System.out.println(threeMinutes.getSeconds());
        System.out.println(threeMinutesTwo.getSeconds());

        //Period 年、月、日角度衡量时间间隔
        Period period = Period.between(LocalDate.now(), LocalDate.now().withDayOfMonth(25));
        System.out.println(period.getDays());

        LocalDate d8 = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        LocalDate d9 = LocalDate.now().with(TemporalAdjusters.firstDayOfNextYear());

        System.out.println(d8);
        System.out.println(d9);
    }

    public static void main(String[] args) {
        new LocalDateTimeTest().localDateTest();
    }
}
